using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Runtime.Serialization;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using OpenComposite.Workflow.Interfaces;
using OpenComposite.Workflow.Extenders;

namespace OpenComposite.Workflow
{
	[
	Serializable(),
	Designer(typeof(LogicalInputActivityDesigner)),
	ToolboxItem(typeof(LogicalInputActivityToolboxItem)),
	ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.event.png"),
	ActivityValidator(typeof(LogicalInputActivityValidator))
	]
	public partial class LogicalInputActivity
		: HandleExternalEventActivity, ITrackingActivity, ILogicalActivity, ITextActivity
	{
		#region Initialization
		public LogicalInputActivity()
		{
		}
		#endregion

		#region Public Members

		#region Properties

		#region ActivitiesClass
		public static DependencyProperty ActivitiesClassProperty = DependencyProperty.Register(
			"ActivitiesClass", typeof(object), typeof(LogicalInputActivity), new PropertyMetadata(null));
		[Category("Workflow Event")]
		public object ActivitiesClass
		{
			get { return base.GetValue(LogicalInputActivity.ActivitiesClassProperty); }
			set { base.SetValue(LogicalInputActivity.ActivitiesClassProperty, value); }
		}
		#endregion

		#region LogicalActivityName
		public static DependencyProperty LogicalActivityNameProperty =
			DependencyProperty.Register("LogicalActivityName", typeof(string),
										typeof(LogicalInputActivity), new PropertyMetadata(null));
		[Browsable(false)]
		[Category("Workflow Event")]
		public string LogicalActivityName
		{
			get { return (string)GetValue(LogicalInputActivity.LogicalActivityNameProperty); }
			set { SetValue(LogicalInputActivity.LogicalActivityNameProperty, value); }
		}
		#endregion

		#region LogicalActivityId
		public static DependencyProperty LogicalActivityIdProperty =
			DependencyProperty.Register("LogicalActivityId", typeof(int),
										typeof(LogicalInputActivity), new PropertyMetadata(null));
		[Browsable(false)]
		[Category("Workflow Event")]
		public int LogicalActivityId
		{
			get { return (int)GetValue(LogicalInputActivity.LogicalActivityIdProperty); }
			set { SetValue(LogicalInputActivity.LogicalActivityIdProperty, value); }
		}
		#endregion

		#region Text
		public static DependencyProperty TextProperty = DependencyProperty.Register(
			"Text", typeof(string), typeof(LogicalInputActivity), new PropertyMetadata(""));
		[Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
		[DefaultValue("")]
		[Browsable(true)]
		public string Text
		{
			get
			{
				string text = (string)base.GetValue(LogicalInputActivity.TextProperty);
				if ( string.IsNullOrEmpty(text) ) return this.Name;
				return text;
			}
			set { base.SetValue(LogicalInputActivity.TextProperty, value); }
		}

		DependencyProperty ITextActivity.GetTextProperty()
		{
			return TextProperty;
		}
		#endregion

		#region EventName
		//[Browsable(false)]
		public override string EventName
		{
			get
			{
				return base.EventName;
			}
			set
			{
				base.EventName = value;
			}
		}
		#endregion

		#region InterfaceType
		//[Browsable(false)]
		public override Type InterfaceType
		{
			get
			{
				return base.InterfaceType;
			}
			set
			{
				base.InterfaceType = value;
			}
		}
		#endregion

		#region HasOutputObject
		public static DependencyProperty HasOutputObjectProperty =
			DependencyProperty.Register("HasOutputObject", typeof(bool),
										typeof(LogicalInputActivity), new PropertyMetadata(DependencyPropertyOptions.Metadata));
#if DEBUG
		[Browsable(true)]
#else
		[Browsable(false)]
#endif
		[Category("Workflow Event")]
		public bool HasOutputObject
		{
			get { return (bool)GetValue(HasOutputObjectProperty); }
			set { SetValue(HasOutputObjectProperty, value); }
		}
		#endregion

		#endregion

		#endregion

		#region ITrackingActivity Members

		public void AddKPIData(string sKPIName, string sKPIValue, string sKPIUnit, string sKPIDataType)
		{
			this.TrackData(sKPIName, new KPITrackingData(sKPIName, sKPIValue, sKPIUnit, sKPIDataType));
		}

		public void AddBusinessObjectData(string sBOName, object sBOValue)
		{
			this.TrackData(sBOName, new BusinessObjectTrackingData(sBOName, sBOValue));
		}

		#endregion
	}

	#region Class: LogicalInputActivityToolboxItem
	[Serializable]
	public class LogicalInputActivityToolboxItem : ActivityToolboxItem
	{
		public LogicalInputActivityToolboxItem()
			: base()
		{
			base.Initialize(typeof(LogicalInputActivity));
			this.DisplayName = "Logical Input";
		}
		public LogicalInputActivityToolboxItem(Type type)
			: base(type)
		{
			this.Initialize(type);
			this.DisplayName = "Logical Input";
		}
		public LogicalInputActivityToolboxItem(SerializationInfo info, StreamingContext context)
		{
			this.Deserialize(info, context);
		}

		private object _tag;
		public object Tag
		{
			get { return _tag; }
			set { _tag = value; }
		}

		public event EventHandler<InitializeLogicalInputActivityEventArgs>
			InitializeLogicalInputActivity = null;

		protected override IComponent[] CreateComponentsCore(IDesignerHost host)
		{
			LogicalInputActivity act = new LogicalInputActivity();
			act.Site = new MySite(host.RootComponent, host.Container, true, null);

			OnInitializeLogicalInputActivity(act);
			CompositeActivity ca = host.RootComponent as CompositeActivity;
			if ( ca != null ) {
				act.Text = WorkflowHelpers.MakeNameWithSpaces(act.Name);
				act.Name = WorkflowHelpers.EnsureUniqueName(ca, act.Name);
			}

			act.Site = null;
			return new IComponent[] { act };
		}
		protected virtual void OnInitializeLogicalInputActivity(LogicalInputActivity logact)
		{
			EventHandler<InitializeLogicalInputActivityEventArgs> tmp = this.InitializeLogicalInputActivity;
			if ( tmp != null )
				tmp(this, new InitializeLogicalInputActivityEventArgs(logact));
		}

	}

	#region Class: InitializeLogicalInputActivityyEventArgs
	[Serializable]
	public class InitializeLogicalInputActivityEventArgs : EventArgs
	{
		public InitializeLogicalInputActivityEventArgs(LogicalInputActivity logact)
		{
			_logact = logact;
		}
		private LogicalInputActivity _logact;
		public LogicalInputActivity LogicalInputActivity
		{
			get { return _logact; }
		}
	}
	#endregion

	#endregion

	#region Class: LogicalInputActivityDesigner
	[
	Serializable,
	ActivityDesignerTheme(typeof(LogicalInputActivityDesignerTheme))
	]
	public class LogicalInputActivityDesigner : ExtendedActivityDesigner
	{
		private Size _minimumSize = new Size(50, 40);

		private string ActivityText
		{
			get { return ( (LogicalInputActivity)Activity ).Text; }
		}

		public override Size MinimumSize
		{
			get { return _minimumSize; }
		}

		protected override void Initialize(Activity activity)
		{
			base.Initialize(activity);
			if ( string.IsNullOrEmpty(this.ActivityText) ) {
				this.Activity.SetValue(LogicalInputActivity.TextProperty, this.Activity.Name);
			}
		}

		protected override void OnActivityChanged(ActivityChangedEventArgs e)
		{
			if ( e.Member.Name == "Text" ) this.PerformLayout();
			base.OnActivityChanged(e);
		}
	}
	#endregion

	#region Class: LogicalInputActivityDesignerTheme
	[Serializable]
	public class LogicalInputActivityDesignerTheme : ActivityDesignerTheme
	{
		public LogicalInputActivityDesignerTheme(WorkflowTheme theme)
			: base(theme)
		{
			base.Initialize();
			this.BorderColor = Color.YellowGreen;

			this.BackColorStart = Color.YellowGreen;
			this.BackColorEnd = Color.White;
			this.BackgroundStyle = System.Drawing.Drawing2D.LinearGradientMode.Vertical;

			this.ForeColor = Color.Black;
		}
	}
	#endregion

	#region Class: LogicalInputActivityValidator : Base: ActivityValidator
	public class LogicalInputActivityValidator : ActivityValidator
	{
		public LogicalInputActivityValidator()
			: base()
		{

		}

		public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
		{
			LogicalInputActivity act = obj as LogicalInputActivity;
			if ( act != null ) {
				ValidationErrorCollection list = new ValidationErrorCollection();
				if ( act.InterfaceType == null ) list.Add(new ValidationError("Interface must be set", 501, false, "InterfaceType"));
				return list;
			} else {
				return base.Validate(manager, obj);
			}
		}

		public override ValidationErrorCollection ValidateProperties(ValidationManager manager, object obj)
		{
			return base.ValidateProperties(manager, obj);
		}
	}
	#endregion
}
